package com.frxuexi.myroom.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Leave
import com.frxuexi.myroom.viewmodel.LeaveViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Composable
fun LeaveListView(navHostController: NavHostController,vm:LeaveViewModel= hiltViewModel()) {
    var uname = MMKV.defaultMMKV().getString("uname","")!!
    val list = vm.getMyLeaves(uname).collectAsLazyPagingItems()
    Column (
        Modifier
            .fillMaxSize()
            .padding(20.dp)){

        BaseTitle(navHostController = navHostController, title = "请假记录", btn = "申请"){
            navHostController.navigate(RoutePath.ROOM_ASKLEAVE)
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 15.dp),
            horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                LeaveItem(entity = it!!,navHostController)
            }
        })
    }
}

@Composable
fun LeaveItem(entity: Leave, navHostController: NavHostController){
    Row(
        Modifier
            .fillMaxWidth()
            .background(colorResource(id = R.color.lightpurple), RoundedCornerShape(10.dp))
            .padding(vertical = 15.dp)
            .clickable {
                val json = Gson().toJson(entity)
                navHostController.navigate(RoutePath.LEAVE_DETAIL + "/$json")
            },
        horizontalArrangement = Arrangement.SpaceAround, verticalAlignment = Alignment.CenterVertically) {
        Text(text = entity.type)
        Text(text = entity.startTime.substring(0,10))
        Text(text = entity.endTime.substring(0,10))
        var color = if(entity.status=="通过") Color.Green else if(entity.status=="拒绝") Color.Red else Color.LightGray
        Text(text = entity.status,
            Modifier
                .background(color, RoundedCornerShape(15.dp))
                .padding(10.dp, 5.dp))
    }
}